package com.zt.english.utils
import android.content.Context
import android.content.res.Resources
import android.graphics.Paint
import android.graphics.Rect
import android.util.TypedValue
import android.widget.TextView
import com.zt.english.R

/**
 * word工具类
 */
class WordUtil(private val mAppContext: Context?) {
    val chapterFontSize: Int //字体大小
    val chapterWordSapce: Int //字体间距
    val chapterLineHeight: Int //每行字体高度
    val chapterPagePadding: Int //页面内边距(包裹Item的左右上下内边距)

    init {
        chapterFontSize = mAppContext!!.resources.getDimension(R.dimen.chapter_word_font).toInt()
        chapterWordSapce = mAppContext.resources.getDimension(R.dimen.chapter_word_space).toInt()
        chapterLineHeight = mAppContext.resources.getDimension(R.dimen.chapter_line_height).toInt()
        chapterPagePadding = mAppContext.resources.getDimension(R.dimen.chapter_page_padding).toInt()
    }

    //获取单个单词宽
    fun getStringWidth(content: String): Int {
        val bounds = Rect()
        val textView = TextView(mAppContext)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, chapterFontSize.toFloat())
        val textPaint: Paint = textView.paint
        textPaint.getTextBounds(content, 0, content.length, bounds)
        return bounds.width() + chapterWordSapce //字体宽度+字体间距 组合成一个单词的最后的宽度
    }
    //获取单个单词宽
    fun getStringWidth2(textView:TextView): Int {
        val bounds = Rect()
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, chapterFontSize.toFloat())
        val textPaint: Paint = textView.paint
        textPaint.getTextBounds(textView.text.toString(), 0, textView.text.length, bounds)
        return bounds.width() + chapterWordSapce //字体宽度+字体间距 组合成一个单词的最后的宽度
    }
    //获取单个标点符号宽
    fun getStringMarkWidth(content: String): Int {
        val bounds = Rect()
        val textView = TextView(mAppContext)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, chapterFontSize.toFloat())
        val textPaint: Paint = textView.paint
        textPaint.getTextBounds(content, 0, content.length, bounds)
        return bounds.width()
    }

    //获取单个单词高
    fun getStringHeight(content: String): Int {
        val bounds = Rect()
        val textView = TextView(mAppContext)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, chapterFontSize.toFloat())
        val textPaint: Paint = textView.paint
        textPaint.getTextBounds(content, 0, content.length, bounds)
        return bounds.height()
    }

    //获取单个页面宽
    val chapterPageWidth: Int
        get() {
            val metrics = Resources.getSystem().displayMetrics
            return metrics.widthPixels - chapterPagePadding * 2
        }
    /**
     * dp2px
     */
    fun convertDpToPixel(dp: Float): Float {
        val resources = mAppContext!!.resources
        val metrics = resources.displayMetrics
        return dp * (metrics.densityDpi / 160f)
    }

    /**
     *px2dp
     */
    fun convertPixelsToDp(px: Float): Int {
        val resources = mAppContext!!.resources
        val metrics = resources.displayMetrics
        val dp = px / (metrics.densityDpi / 160f)
        return dp.toInt()
    }
}